﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Xml.Linq;
using DGSFramework;

namespace Framework.LoginAdmin
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ac"] != null)
            {
                FormsAuthentication.SignOut();
            }
            else
            {
                if (!IsPostBack)
                {
                    MembershipUser someUser = Membership.GetUser();
                    if (someUser == null)
                    {
                        FormsAuthentication.SignOut();
                    }
                    else
                    {
                        XDocument xDocWebconfig = XDocument.Load(HttpContext.Current.Server.MapPath("\\WebmasterTool\\web.config"));
                        XElement xEAllowRoles = xDocWebconfig.Descendants("authorization").Descendants("allow").Where(ra => ra.Attribute("roles") != null).FirstOrDefault();
                        if (Roles.GetRolesForUser(someUser.UserName).All(rl => xEAllowRoles.Attribute("roles").Value.Contains(rl)))
                        {
                            CreateDBLog(null, null, TypeAction.None, "Đăng nhập vào hệ thống quản trị", "", "", "", "", "", "", "", "", someUser.UserName);
                            Response.Redirect(Globals.ApplicationPath + "WebmasterTool/Default.aspx");
                        }
                        else
                        {
                            lblMsg.InnerText = "Tên đăng nhập này không có quyền vào trang quản trị";
                        }
                    }
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
         
            if (Membership.ValidateUser(txtUsername.Text.Trim(), txtPassword.Text))
            {
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, txtUsername.Text, DateTime.Now, DateTime.Now.AddDays(7), chkRememberMe.Checked, txtUsername.Text, FormsAuthentication.FormsCookiePath);
                string encTicket = FormsAuthentication.Encrypt(ticket);
                Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
                if (Request.QueryString["ReturnUrl"] != null)
                {
                    CreateDBLog(null, null, TypeAction.None, "Đăng nhập vào hệ thống quản trị", "", "", "", "", "", "", "", "", txtUsername.Text);
                    Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text, chkRememberMe.Checked), false);
                }
                else
                {
                    CreateDBLog(null, null, TypeAction.None, "Đăng nhập vào hệ thống quản trị", "", "", "", "", "", "", "", "", txtUsername.Text);
                    FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, chkRememberMe.Checked);
                }
            }
            else
            {
                MembershipUser someUser = Membership.GetUser(txtUsername.Text.Trim());
                if (someUser != null && someUser.IsLockedOut == true)
                {
                    lblMsg.InnerText = "Tài khoản tạm thời bị khoá";
                }
                else
                {
                    lblMsg.InnerText = "Tên đăng nhập hoặc mật khẩu không hợp lệ.";
                }
            }
        }

        public void CreateDBLog(int? postID, int? postCateID, TypeAction typeAction, string title, string _abstract, string description,
        string releaseDate, string expireDate, string isApproved, string statusID, string metaDescription, string metaKeywords, string actionBy)
        {
            DGSFrameworkEntities entity = new DGSFrameworkEntities();

            udv_Log log = new udv_Log();
            log.PostID = postID;
            log.PostCateID = postCateID;
            log.TypeAction = (int)typeAction;
            log.Title = title;
            log.Abstract = _abstract;
            log.Description = description;
            log.ReleaseDate = releaseDate;
            log.ExpireDate = expireDate;
            log.IsApproved = isApproved;
            log.StatusID = statusID;
            log.MetaDescription = metaDescription;
            log.MetaKeywords = metaKeywords;
            log.ActionedDate = DateTime.Now;
            log.ActionBy = actionBy;
            log.IpAdress = Globals.GetClientIPAddress();

            entity.AddToudv_Log(log);
            entity.SaveChanges();
        }
    }
}